package com.liuhaha.auth.domain.service.impl;

import com.alibaba.fastjson.JSON;
import com.liuhaha.auth.domain.convert.AuthUserBOConverter;
import com.liuhaha.auth.domain.entity.AuthUserBO;
import com.liuhaha.auth.domain.service.AuthUserDomainService;
import com.liuhaha.auth.infra.basic.entity.AuthUser;
import com.liuhaha.auth.infra.basic.service.AuthUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author liuhaha
 * @version 1.0
 * @date 2024-05-19 17:58
 */
@Service("authUserDomainService")
@Slf4j
public class AuthUserDomainServiceImpl implements AuthUserDomainService {

    @Resource
    private AuthUserService authUserService;
    @Override
    public Boolean register(AuthUserBO authUserBO) {
        if (log.isInfoEnabled()) {
            log.info("UserController.register.authUserBO:{}", JSON.toJSONString(authUserBO));
        }
        AuthUser authUser = AuthUserBOConverter.INSTANCE.convertBOToEntity(authUserBO);
        int count = authUserService.insert(authUser);
        return count > 0;
    }

    @Override
    public Boolean update(AuthUserBO authUserBO) {
        if (log.isInfoEnabled()) {
            log.info("UserController.update.authUserBO:{}", JSON.toJSONString(authUserBO));
        }
        AuthUser authUser = AuthUserBOConverter.INSTANCE.convertBOToEntity(authUserBO);
        int count = authUserService.updateByUserName(authUser);
        return count > 0;
    }

    @Override
    public AuthUserBO getUserInfo(AuthUserBO authUserBO) {
        if (log.isInfoEnabled()) {
            log.info("UserController.getUserInfo.authUserBO:{}", JSON.toJSONString(authUserBO));
        }
        AuthUser authUser = AuthUserBOConverter.INSTANCE.convertBOToEntity(authUserBO);
        List<AuthUser> authUserList = authUserService.queryByCondition(authUser);
        if (authUserList.isEmpty()){
            return new AuthUserBO();
        }
        AuthUser user = authUserList.get(0);
        return AuthUserBOConverter.INSTANCE.convertEntityToBo(user);
    }

    @Override
    public Boolean delete(AuthUserBO authUserBO) {
        return this.authUserService.delete;
    }
}
